package com.zxpt.quetion.controller;

import com.zxpt.quetion.pojovo.QuMdItVo;
import com.zxpt.quetion.service.IQuMdItService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import  com.zxpt.quetion.common.Result;
import  com.zxpt.quetion.utils.JsonMapper;

/**
* 所在包：com.zxpt.quetion.controller.QuMdItController
* 文件名：QuMdItController
* 版权：稻草人.
* 描述：
* 修改人：huizi
* 修改时间：
*/
@RestController
@RequestMapping("/quMdIt")
public class QuMdItController {

    @Autowired
    private IQuMdItService quMdItService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public Result<PageInfo<QuMdItVo>> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Result<PageInfo<QuMdItVo>> result = new Result<>();
        try {
            Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
            PageInfo<QuMdItVo> pageInfo = quMdItService.getPage(query, page, rows);
            result.setData(pageInfo);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result.error(msg);
        }
        return result;
    }

    /**
    * 新增数据
    */
    @PostMapping("/getById")
    public Result<Object> getById (String id) {
        Result<Object> result = new Result<>();
        try {
            QuMdItVo quMdItVo = quMdItService.getById(id);
            result.setData(quMdItVo);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result.error(msg);
        }
        return result;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public Result<Object> add (@RequestBody QuMdItVo quMdItVo) {
        Result<Object> result = new Result<>();
        try {
            quMdItService.add(quMdItVo);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result.error(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public Result<Object> update (@RequestBody QuMdItVo quMdItVo) {
        Result<Object> result = new Result<>();
        try {
            quMdItService.update(quMdItVo);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result.error(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public Result<Object> delById (String id) {
        Result<Object> result = new Result<>();
        try {
            quMdItService.delById(id);
            result.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result.error(msg);
        }
        return result;
    }
}